var _refreshText//下拉刷新参数
var _startPos
var _transitionHeight

/**
 * 触屏开始事件
 * @param id 下拉刷新的ID;  注意ID也是一个class
 */
function touchRefresh(id) {
  var element = document.getElementById(id),
    _refreshText = document.querySelector('.'+id),
    _startPos = 0
  _transitionHeight = 0

  element.addEventListener('touchstart', function(e) {
    _refreshStatus = false;
    if (_refreshStatus) {
      e.preventDefault()
      console.log(1111)
      return
    }
    console.log(2222)
    _startPos = e.touches[0].pageY
    element.style.position = 'relative'
    element.style.transition = 'transform 0s'
  }, false)

  element.addEventListener('touchmove', function(e) {
    if (_refreshStatus) {
      e.preventDefault()
      console.log(3333)
      return
    }

    console.log(4444)
    _transitionHeight = e.touches[0].pageY - _startPos
    element.style.transition = 'transform 0.3s ease-out 0s'
    _refreshText.style.textAlign = 'center'
    _refreshText.style.height = _transitionHeight +'px'

    if (_transitionHeight > 30 && _transitionHeight < 360) {
      _refreshText.innerText = '下拉刷新'

      if (_transitionHeight > 55) {
        _refreshText.innerText = '松开刷新数据'
      }
    }
  }, false)
}

/**
 * 触屏结束事件
 * @param id 下拉刷新的ID  注意ID也是一个class
 * @param callback 数据刷新加载函数
 * @param data 数据刷新加载函数参数
 */
function touchEndDown(id, callback, data) {
  var element = document.getElementById(id)
  element.addEventListener('touchend', function(e) {
    if (_refreshStatus) {
      e.preventDefault()
      console.log(5555)
      return
    }
    console.log(6666 +"--" +  _transitionHeight)

    if (_transitionHeight > 55) {
      var refreshText = document.querySelector('.'+id)
      refreshText.innerText = '加载中...'

      //释放后先回弹到 55高
      //refreshDeal(id, 55)

      //数据刷新加载，加载完毕后需要再次调用回弹函数
      callback(data)
    } else {
      refreshDeal(id)
    }
  }, false)
}

/**
 * 页面回弹
 * @param id 下拉刷新的ID 注意ID也是一个class
 * @param hei 刷新加载样式高度，目前默认为55px
 */
function refreshDeal(id, hei) {
  _refreshText = document.querySelector('.'+id)
  if (Utils.isEmpty(hei)) {
    hei = 0
    $('.'+id).animate({height: 0}, 1000, 'linear', function (e) {
      _refreshText.innerText = ''
    })
  } else {
    $('.'+id).animate({height: 55}, 1000, 'linear', function () {
    })
  }

  console.log(7777)
  _refreshStatus = true;
  _transitionHeight = hei
}